﻿using System;
using System.Collections.Generic;
using System.Windows;
using IsoStorageManagerLib;

namespace IsoStorageManagerApp
{
    public partial class MainPage 
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private async void SaveASButton_OnClick(object sender, RoutedEventArgs e)
        {/*
            var strOrig = GenerateRandomString(1024);
            var filename = "Folder1//Folder2//Text";

            var res1 = await IsoStorageManager.WriteTextEx(filename, strOrig);

            var storedStr = await IsoStorageManager.ReadTextEx(filename);

            MessageBox.Show(strOrig.Equals(storedStr)
                ? "String was restored successfully"
                : "Something went wrong");
          */
        }

        private async void SaveButton_OnClick(object sender, RoutedEventArgs e)
        {
            /*
            var order = GenerateOrder();
            var fileName = "Folder//TestFile1";

            var res1 = await IsoStorageManager.WriteJsonEx(fileName, order);

            var storedOrder = await IsoStorageManager.ReadJsonEx<Order>(fileName);

            // NOTE: add a breakpoint here to compare the fields
            MessageBox.Show(order.Equals(storedOrder) 
                ? "Objects was restored successfully" 
                : "Something went wrong");
            */
        }

        private void SaveSyncButton_OnClick(object sender, RoutedEventArgs e)
        {
            
            var order = GenerateOrder();
            var fileName = "Folder//TestFile2";
            
            IsoStorageManager.WriteJson(fileName, order);

            var storedOrder = IsoStorageManager.ReadJson<Order>(fileName);
            
            // NOTE: add a breakpoint here to compare the fields
            MessageBox.Show(order.Equals(storedOrder)
                ? "Objects was restored successfully synchronously"
                : "Something went wrong");
            
        }

        private async void AsyncReaderWriterLock(object sender, RoutedEventArgs e)
        {
            /*
            var fileName1 = "Folder//TestFile1.txt";
            var fileName2 = "Folder//TestFile2.txt";
            var fileName3 = "Folder//TestFile3.txt";
            var fileName4 = "Folder//TestFile4.txt";
            var fileName5 = "Folder//TestFile5.txt";

            SimpleLogger.WriteLine("Starting generation...");
            
            var list = new List<Order>();
            //for (var i = 0; i < 10; i++)
                list.Add(GenerateOrder());
            
            
            SimpleLogger.WriteLine("Writing 5 times the same file...");

            var res1 = await IsoStorageManager.WriteJsonEx(fileName1, list);
            var res2 = await IsoStorageManager.WriteJsonEx(fileName1, list);
            var res3 = await IsoStorageManager.WriteJsonEx(fileName1, list);
            var res4 = await IsoStorageManager.WriteJsonEx(fileName1, list);
            var res5 = await IsoStorageManager.WriteJsonEx(fileName1, list);
            

            SimpleLogger.WriteLine("Writing 5 different files");

            var res11 = await IsoStorageManager.WriteJsonEx(fileName1, list);
            var res12 = await IsoStorageManager.WriteJsonEx(fileName2, list);
            var res13 = await IsoStorageManager.WriteJsonEx(fileName3, list);
            var res14 = await IsoStorageManager.WriteJsonEx(fileName4, list);
            var res15 = await IsoStorageManager.WriteJsonEx(fileName5, list);

            SimpleLogger.WriteLine("Reading 5 times the same");

            var res21 = await IsoStorageManager.ReadJsonEx<List<Order>>(fileName1);
            var res22 = await IsoStorageManager.ReadJsonEx<List<Order>>(fileName1);
            var res23 = await IsoStorageManager.ReadJsonEx<List<Order>>(fileName1);
            var res24 = await IsoStorageManager.ReadJsonEx<List<Order>>(fileName1);
            var res25 = await IsoStorageManager.ReadJsonEx<List<Order>>(fileName1);

            SimpleLogger.WriteLine("Reading 5 times different");

            var res31 = await IsoStorageManager.ReadJsonEx<List<Order>>(fileName1);
            var res32 = await IsoStorageManager.ReadJsonEx<List<Order>>(fileName2);
            var res33 = await IsoStorageManager.ReadJsonEx<List<Order>>(fileName3);
            var res34 = await IsoStorageManager.ReadJsonEx<List<Order>>(fileName4);
            var res35 = await IsoStorageManager.ReadJsonEx<List<Order>>(fileName5);


            SimpleLogger.WriteLine("Done");
            */
        }

        private async void AsyncMutex(object sender, RoutedEventArgs e)
        {
            var fileName1 = "FolderExSafe//TestFile1.txt";
            var fileName2 = "FolderExSafe//TestFile2.txt";
            var fileName3 = "FolderExSafe//TestFile3.txt";
            var fileName4 = "FolderExSafe//TestFile4.txt";
            var fileName5 = "FolderExSafe//TestFile5.txt";

            SimpleLogger.WriteLine("Starting generation...");

            var list = new List<Order>();
            //for (var i = 0; i < 10; i++)
                list.Add(GenerateOrder());


            SimpleLogger.WriteLine("Writing 5 times the same file...");

            var res1 = await IsoStorageManager.WriteJsonAsync(fileName1, list);
            var res2 = await IsoStorageManager.WriteJsonAsync(fileName1, list);
            var res3 = await IsoStorageManager.WriteJsonAsync(fileName1, list);
            var res4 = await IsoStorageManager.WriteJsonAsync(fileName1, list);
            var res5 = await IsoStorageManager.WriteJsonAsync(fileName1, list);


            SimpleLogger.WriteLine("Writing 5 different files");

            var res11 = await IsoStorageManager.WriteJsonAsync(fileName1, list);
            var res12 = await IsoStorageManager.WriteJsonAsync(fileName2, list);
            var res13 = await IsoStorageManager.WriteJsonAsync(fileName3, list);
            var res14 = await IsoStorageManager.WriteJsonAsync(fileName4, list);
            var res15 = await IsoStorageManager.WriteJsonAsync(fileName5, list);

            SimpleLogger.WriteLine("Reading 5 times the same");

            var res21 = await IsoStorageManager.ReadJsonAsync<List<Order>>(fileName1);
            var res22 = await IsoStorageManager.ReadJsonAsync<List<Order>>(fileName1);
            var res23 = await IsoStorageManager.ReadJsonAsync<List<Order>>(fileName1);
            var res24 = await IsoStorageManager.ReadJsonAsync<List<Order>>(fileName1);
            var res25 = await IsoStorageManager.ReadJsonAsync<List<Order>>(fileName1);

            SimpleLogger.WriteLine("Reading 5 times different");

            var res31 = await IsoStorageManager.ReadJsonAsync<List<Order>>(fileName1);
            var res32 = await IsoStorageManager.ReadJsonAsync<List<Order>>(fileName2);
            var res33 = await IsoStorageManager.ReadJsonAsync<List<Order>>(fileName3);
            var res34 = await IsoStorageManager.ReadJsonAsync<List<Order>>(fileName4);
            var res35 = await IsoStorageManager.ReadJsonAsync<List<Order>>(fileName5);
            
            SimpleLogger.WriteLine("Done");
        }
        
        private async void AsyncCS(object sender, RoutedEventArgs e)
        {
            /*
            var fileName1 = "FolderExCs//TestFile1.txt";
            var fileName2 = "FolderExCs//TestFile2.txt";
            var fileName3 = "FolderExCs//TestFile3.txt";
            var fileName4 = "FolderExCs//TestFile4.txt";
            var fileName5 = "FolderExCs//TestFile5.txt";

            SimpleLogger.WriteLine("Starting generation...");

            var list = new List<Order>();
            //for (var i = 0; i < 10; i++)
            list.Add(GenerateOrder());


            SimpleLogger.WriteLine("Writing 5 times the same file...");

            var res1 = await IsoStorageManager.WriteJsonExCs(fileName1, list);
            var res2 = await IsoStorageManager.WriteJsonExCs(fileName1, list);
            var res3 = await IsoStorageManager.WriteJsonExCs(fileName1, list);
            var res4 = await IsoStorageManager.WriteJsonExCs(fileName1, list);
            var res5 = await IsoStorageManager.WriteJsonExCs(fileName1, list);


            SimpleLogger.WriteLine("Writing 5 different files");

            var res11 = await IsoStorageManager.WriteJsonExCs(fileName1, list);
            var res12 = await IsoStorageManager.WriteJsonExCs(fileName2, list);
            var res13 = await IsoStorageManager.WriteJsonExCs(fileName3, list);
            var res14 = await IsoStorageManager.WriteJsonExCs(fileName4, list);
            var res15 = await IsoStorageManager.WriteJsonExCs(fileName5, list);

            SimpleLogger.WriteLine("Reading 5 times the same");

            var res21 = await IsoStorageManager.ReadJsonExCs<List<Order>>(fileName1);
            var res22 = await IsoStorageManager.ReadJsonExCs<List<Order>>(fileName1);
            var res23 = await IsoStorageManager.ReadJsonExCs<List<Order>>(fileName1);
            var res24 = await IsoStorageManager.ReadJsonExCs<List<Order>>(fileName1);
            var res25 = await IsoStorageManager.ReadJsonExCs<List<Order>>(fileName1);

            SimpleLogger.WriteLine("Reading 5 times different");

            var res31 = await IsoStorageManager.ReadJsonExCs<List<Order>>(fileName1);
            var res32 = await IsoStorageManager.ReadJsonExCs<List<Order>>(fileName2);
            var res33 = await IsoStorageManager.ReadJsonExCs<List<Order>>(fileName3);
            var res34 = await IsoStorageManager.ReadJsonExCs<List<Order>>(fileName4);
            var res35 = await IsoStorageManager.ReadJsonExCs<List<Order>>(fileName5);

            SimpleLogger.WriteLine("Done");
            */
        }


        private Order GenerateOrder()
        {
            Random rand = new Random();

            var item00 = new ProductEntity();
            item00.Description = "item00";
            item00.MyInt = rand.Next(10000);
            item00.MyDouble = 0.0;


            var item01 = new ProductEntity();
            item01.Description = "item01";
            item01.MyInt = rand.Next(10000);
            item01.MyDouble = 0.1;

            var item10 = new ProductEntity();
            item10.Description = "item10";
            item10.MyInt = rand.Next(10000);
            item10.MyDouble = 1.0;


            var list0 = new ProductList();
            list0.Name = "List0";
            list0.List = new List<ProductEntity>();
            list0.List.Add(item00);
            list0.List.Add(item01);

            var list1 = new ProductList();
            list1.Name = "List1";
            list1.List = new List<ProductEntity>();
            list1.List.Add(item10);


            var order = new Order();
            order.Date = DateTime.UtcNow;
            order.Lists = new List<ProductList>();
            order.Lists.Add(list0);
            order.Lists.Add(list1);
            order.Name = new String('a', 1024*1024*5);

            return order;
        }

        public static string GenerateRandomString(int length)
        {
            var random = new Random();
            var randomString = "";

            for (var i = 0; i < length; i++)
            {
                var randNumber = random.Next(1, 3) == 1 ? random.Next(97, 123) : random.Next(48, 58);
                randomString = randomString + (char)randNumber;
            }

            return randomString;
        }
    }
}